package engine.core
{
	public class RxSemaphore
	{
		private var m_count:int;
		private var m_callbacks:Vector.<RxCallback>;
		
		public function RxSemaphore()
		{
			this.m_count = 0;
			this.m_callbacks = new Vector.<RxCallback>;
		}
		
		public function AddCallback(param1:RxCallback) : void
		{
			this.m_callbacks.push(param1);
			return;
		}
		
		public function WaitForSignal() : void
		{
			this.m_count = this.m_count + 1;
		}
		
		public function Signal() : void
		{
			this.m_count = this.m_count - 1;
			if (this.m_count < 0)
			{
				RxLog.Error("Semaphore underflow");
			}
			if (this.m_count == 0)
			{
				for each (var callback:RxCallback in this.m_callbacks)
				{
					
					callback.m_function();
				}
				this.m_callbacks.length = 0;
			}
		}
		
		public function Delete() : void
		{
			for each (var callback:RxCallback in this.m_callbacks)
			{
				
				callback.Delete();
			}
			this.m_callbacks.length = 0;
			this.m_callbacks = null;
		}
	}
}